﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Teriri.File.UniversalLib.Helper
{
    /// <summary>
    /// 简单日志帮助类
    /// </summary>
    public class LoggerHelper
    {
        private static ConcurrentQueue<string> LogQueue;

        static LoggerHelper()
        {
            LogQueue = new ConcurrentQueue<string>();

            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(10);

                    try
                    {
                        if (LogQueue.TryDequeue(out var log))
                            await WriteLog(log);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            });
        }

        public static async void Log<T>(Exception ex)
        {
            await Task.Yield();

            var log = @$"
------------------------------------------------------------------------
    时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}
    类型：{typeof(T).FullName}
    异常信息：{ex?.Message ?? "-"}
    异常堆栈：{ex?.StackTrace ?? "-"}";

            while (true)
            {
                if (LogQueue != null)
                {
                    LogQueue.Enqueue(log);
                    return;
                }
            }
        }

        public static async void Log<T>(string msg, Exception ex = null)
        {
            await Task.Yield();

            var log = @$"
------------------------------------------------------------------------
    时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}
    类型：{typeof(T).FullName}
    日志信息：{msg ?? "-"}
    异常信息：{ex?.Message ?? "-"}
    异常堆栈：{ex?.StackTrace ?? "-"}";

            while (true)
            {
                if (LogQueue != null)
                {
                    LogQueue.Enqueue(log);
                    return;
                }
            }
        }

        private static async Task WriteLog(string log)
        {
            #region 文件打印

            string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            var dayInfo = DateTime.Now.ToString("yyyy-MM-dd");

            var filePath = Path.Combine(folderPath, dayInfo + ".log");
            if (!System.IO.File.Exists(filePath))
                System.IO.File.Create(filePath).Dispose();

            using var sw = new StreamWriter(filePath, true);
            sw.Write(Environment.NewLine);
            sw.Write(log);

            #endregion

            #region 控制台打印

            if (log.Contains("异常信息：-"))
                Console.ForegroundColor = ConsoleColor.Green;
            else
                Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine(log);
            Console.ForegroundColor = ConsoleColor.White;

            #endregion
        }
    }
}
